package com.zllkj.mm.module.home.fragments;

import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.android.material.tabs.TabLayout;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.zllkj.mm.R;
import com.zllkj.mm.api.network.RetrofitHelper;
import com.zllkj.mm.api.network.RxHelper;
import com.zllkj.mm.api.network.subscriber.ProgressSubscriber;
import com.zllkj.mm.base.BaseTitleFragment;
import com.zllkj.mm.common.L;
import com.zllkj.mm.model.entity.GoodsSelfListS;

import java.util.ArrayList;
import java.util.HashMap;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;


/**
 * 商品管理
 * Created by leon_ok.
 */

public class GoodsListFragment extends BaseTitleFragment {

    @BindView(R.id.tl)
    TabLayout tl;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.springView)
    SpringView springView;
    @BindView(R.id.add)
    View add;

    public static GoodsListFragment newInstance() {
        return new GoodsListFragment();
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_goodslist;
    }

    private ArrayList<GoodsSelfListS.RecordsBean> contents = new ArrayList<>();
    private BaseQuickAdapter adapterContent;

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setCommonTitle("商品管理");
        tl.addTab(tl.newTab().setText("全部"));
        tl.addTab(tl.newTab().setText("已上架"));
        tl.addTab(tl.newTab().setText("审核中"));
        tl.addTab(tl.newTab().setText("已下架"));
        tl.addTab(tl.newTab().setText("已封禁"));
        tl.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                type = tab.getPosition() == 1 ? 3 : tab.getPosition() == 2 ? 1 : tab.getPosition() == 3 ? 4 : tab.getPosition() == 4 ? 5 : 0;//1-待审核、2-审核驳回、3-上架、4-下架、5-已封禁
                pageNum = 1;
                requestData();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        rv.setAdapter(adapterContent = new BaseQuickAdapter<GoodsSelfListS.RecordsBean, BaseViewHolder>(R.layout.item_goodsselflist, contents) {
                    @Override
                    protected void convert(BaseViewHolder helper, GoodsSelfListS.RecordsBean item) {
                        String[] split = item.goodsPic.split(",");
                        L.setImg(split[0], helper.getView(R.id.ivGoods));
                        helper
                                .setText(R.id.goodsName, item.goodsName)
                                .setText(R.id.price, item.unitPrice + "元")
                        ;
                    }
                }
        );
        springView.setHeader(new DefaultHeader(getContext()));
        springView.setFooter(new DefaultFooter(getContext()));
        springView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                pageNum = 1;
                requestData();
            }

            @Override
            public void onLoadmore() {
                pageNum++;
                isClear = false;
                requestData();
            }
        });
    }
    int pageNum = 1;
    boolean isClear = true;
    public int type = 0;
    void requestData() {
        RetrofitHelper.getInstance().getApi()
                        .goodsSelfList(L.sign(new HashMap() {{
                            if (type != 0)
                                put("goodsStatus", type);
                            put("currentPage", pageNum);
//                        put("pageSize", 5);
                        }}))
                        .compose(RxHelper.<GoodsSelfListS>defaultHandleResult())
                        .subscribe(new ProgressSubscriber<GoodsSelfListS>(getContext()) {
                            @Override
                            protected void onSucc(GoodsSelfListS o) {
                                springView.onFinishFreshAndLoad();
                                if (isClear) contents.clear();
                                isClear = true;
                                contents.addAll(o.records);
                                adapterContent.notifyDataSetChanged();
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
                                springView.onFinishFreshAndLoad();
                            }
                        })
        ;
    }

    @Override
    public void onResume() {
        super.onResume();

        requestData();

    }

    @OnClick(R.id.add)
    public void onViewClicked() {
        addFragment(GoodsAddFragment.newInstance());
    }
}

